package com.kgcx.opc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.kgcx.common.rabbitmq.bean.Message;
import com.kgcx.opc.common.dto.DataResponse;
import com.kgcx.opc.common.utils.bean.BeanUtil;
import com.kgcx.opc.common.utils.http.HttpUtils;
import com.kgcx.opc.common.utils.io.FtpsUtils;
import com.kgcx.opc.constans.MsgConstants;
import com.kgcx.opc.constans.UrlConstants;
import com.kgcx.opc.entity.UrlFile;
import com.kgcx.opc.service.AssignService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.CacheControl;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 何凯
 * @version 0.1
 * @description
 * @time 2017/11/14 11:30
 * @since 0.1
 */
@Slf4j
@Service
public class AssignServiceImpl implements AssignService{
    //private static final String URL ="http://172.16.5.101:8080";
    //private static final String URL_END ="?company=konggang";
    private static final String URL ="http://localhost:18088/api/test/reveive";
    private static final String SOURCE ="0";
    @Autowired
    private BeanUtil beanUtil;
    @Autowired
    private UrlFile urlFile;
    /**
     * 对过来的消息进行处理
     * @param message
     * @return
     */
    @Override
    public Object forwordApi(Message message) throws Exception{
        Map<String,Object> paramMap= new HashMap<>();
        Map<String,Object> ipTypeMap= new HashMap<>();
        paramMap.put("Source",SOURCE);
        paramMap.put("CompanyId", BeanUtil.companyId);
        paramMap.put("IPCType", message.getType());
        StringBuilder sb = new StringBuilder(urlFile.getUrlBegin());
        switch (message.getType()){
            case MsgConstants.BASEINFO_COMPANY :
                sb.append(UrlConstants.BASEINFO_COMPANY);
                ipTypeMap=beanUtil.setBeanByCompany(message);
                break;
            case MsgConstants.BASEINFO_COMPANY_STAT :
                sb.append(UrlConstants.BASEINFO_COMPANY_STAT);
                ipTypeMap=beanUtil.setBeanByCompanyStat(message);
                break;
            case MsgConstants.BASEINFO_COMPANY_PAY :
                sb.append(UrlConstants.BASEINFO_COMPANY_PAY);
                ipTypeMap=beanUtil.setBeanByCompanyPay(message);
                break;
            case MsgConstants.BASEINFO_COMPANY_SERVICE :
                sb.append(UrlConstants.BASEINFO_COMPANY_SERVICE);
                ipTypeMap=beanUtil.setBeanByCompanyService(message);
                break;
            case MsgConstants.BASEINFO_COMPANY_PERMIT :
                sb.append(UrlConstants.BASEINFO_COMPANY_PERMIT);
                ipTypeMap=beanUtil.setBeanByCompanyPermit(message);
                break;
            case MsgConstants.BASEINFO_COMPANY_FARE :
                sb.append(UrlConstants.BASEINFO_COMPANY_FARE);
                ipTypeMap=beanUtil.setBeanByCompanyFare(message);
                break;
            case MsgConstants.BASEINFO_VEHICLE :
                sb.append(UrlConstants.BASEINFO_VEHICLE);
                ipTypeMap=beanUtil.setBeanByBaseInfoVehicle(message);
                break;
            case MsgConstants.BASEINFO_VEHICLE_INSURANCE :
                sb.append(UrlConstants.BASEINFO_VEHICLE_INSURANCE);
                ipTypeMap=beanUtil.setBeanByBaseInfoVehicleInsurance(message);
                break;
            case MsgConstants.BASEINFO_VEHICLE_TOTALMILE :
                sb.append(UrlConstants.BASEINFO_VEHICLE_TOTALMILE);
                ipTypeMap=beanUtil.setBeanByBaseInfoVehicleTotalMile(message);
                break;
            case MsgConstants.BASEINFO_DRIVER :
                sb.append(UrlConstants.BASEINFO_DRIVER);
                ipTypeMap=beanUtil.setBeanByBaseInfoDriver(message);
                break;
            case MsgConstants.BASEINFO_DRIVEREDUCATE :
                sb.append(UrlConstants.BASEINFO_DRIVEREDUCATE);
                ipTypeMap=beanUtil.setBeanByBaseInfoDriverEducate(message);
                break;
            case MsgConstants.BASEINFO_DRIVERAPP :
                sb.append(UrlConstants.BASEINFO_DRIVERAPP);
                ipTypeMap=beanUtil.setBeanByBaseInfoDriverApp(message);
                break;
            case MsgConstants.BASEINFO_DRIVERSTAT :
                sb.append(UrlConstants.BASEINFO_DRIVERSTAT);
                ipTypeMap=beanUtil.setBeanByBaseInfoDriverStat(message);
                break;
            case MsgConstants.BASEINFO_PASSENGER :
                sb.append(UrlConstants.BASEINFO_PASSENGER);
                ipTypeMap=beanUtil.setBeanByBaseInfoPassenger(message);
                break;
            case MsgConstants.ORDER_CREATE :
                sb.append(UrlConstants.ORDER_CREATE);
                ipTypeMap=beanUtil.setBeanByOrderCreat(message);
                break;
            case MsgConstants.ORDER_MATCH :
                sb.append(UrlConstants.ORDER_MATCH);
                ipTypeMap=beanUtil.setBeanByOrderMatch(message);
                break;
            case MsgConstants.ORDER_CANCEL :
                sb.append(UrlConstants.ORDER_CANCEL);
                ipTypeMap=beanUtil.setBeanByOrderCancel(message);
                break;
            case MsgConstants.OPERATE_LOGIN :
                sb.append(UrlConstants.OPERATE_LOGIN);
                ipTypeMap=beanUtil.setBeanByOperateLogin(message);
                break;
            case MsgConstants.OPERATE_LOGOUT :
                sb.append(UrlConstants.OPERATE_LOGOUT);
                ipTypeMap=beanUtil.setBeanByOperateLogout(message);
                break;
            case MsgConstants.OPERATE_DEPART :
                sb.append(UrlConstants.OPERATE_DEPART);
                ipTypeMap=beanUtil.setBeanByOperateDepart(message);
                break;
            case MsgConstants.OPERATE_ARRIVE :
                sb.append(UrlConstants.OPERATE_ARRIVE);
                ipTypeMap=beanUtil.setBeanByOperateArrive(message);
                break;
            case MsgConstants.OPERATE_PAY :
                sb.append(UrlConstants.OPERATE_PAY);
                ipTypeMap=beanUtil.setBeanByOperatePay(message);
                break;
            case MsgConstants.POSITION_DRIVER :
                sb.append(UrlConstants.POSITION_DRIVER);
                ipTypeMap=beanUtil.setBeanByPositionDriver(message);
                break;
            case MsgConstants.POSITION_VEHICLE :
                sb.append(UrlConstants.POSITION_VEHICLE);
                ipTypeMap=beanUtil.setBeanByPositionVehicle(message);
                break;
            case MsgConstants.RATED_PASSENGER :
                sb.append(UrlConstants.RATED_PASSENGER);
                ipTypeMap=beanUtil.setBeanByRatedPassenger(message);
                break;
            case MsgConstants.RATED_PASSENGERCOMPLAINT :
                sb.append(UrlConstants.RATED_PASSENGERCOMPLAINT);
                ipTypeMap=beanUtil.setBeanByRatedPassengerComplaint(message);
                break;
            case MsgConstants.RATED_DRIVERPUNISH :
                sb.append(UrlConstants.RATED_DRIVERPUNISH);
                ipTypeMap=beanUtil.setBeanByRatedDriverPunish(message);
                break;
            case MsgConstants.RATED_DRIVER :
                sb.append(UrlConstants.RATED_DRIVER);
                ipTypeMap=beanUtil.setBeanByRatedDriver(message);
                break;
        }
        sb.append(urlFile.getUrlEnd());
        log.debug("URL:"+sb.toString());
        paramMap.put(message.getType(),ipTypeMap);
        //定位信息采用批量
        List<Map<String,Object>> list = new ArrayList<>();
        switch (message.getType()){
            case MsgConstants.POSITION_DRIVER :
                list.add(ipTypeMap);
                paramMap.put(message.getType(),list);
                break;
            case MsgConstants.POSITION_VEHICLE :
                list.add(ipTypeMap);
                paramMap.put(message.getType(),list);
                break;

        }
        //log.debug("URL:++++++++:"+urlFile.getUrlEnd());
        sendMsg(sb.toString(),paramMap);
        log.debug("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
        sendMsg(URL,paramMap);
        // log.debug("22222++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

        //sendPost(sb.toString(),paramMap);
        return DataResponse.createSuccessResponse(DataResponse.SUCCESS_CODE);
    }

    @Override
    public void uploadImag(String name) {
        FtpsUtils.getInstance().uploadFileByRemote(name);
    }

    /**
     * okhttp 发送请求
     */
    public void sendMsg(String url,Map<String, Object> paramMap){
        try {
            Map<String, String> headerMap = new HashMap<>();
            headerMap.put("accept","*/*");
            headerMap.put("connection","Keep-Alive");
            headerMap.put("Content-Type","application/json");
            CacheControl cacheControl =null;
            Callback callback = new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    log.info("fail!");
                    e.printStackTrace();
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    log.info("success!!!");
                    log.info("response++++++++++++code:"+response.code()+"-----"+response.toString());
                }
            };
            String tag ="";
            HttpUtils.getInstance().asyncPostJSONRequest(url,headerMap,paramMap,cacheControl,callback,tag);
        }catch (Exception e){
            log.error("e:"+e.getMessage());
        }
    }

    /**
     * HttpURLConnection 发送请求
     */
    public void sendPost(String url,Map<String, Object> paramMap){
        JSONObject obj = new JSONObject();
        String json = JSONObject.toJSONString(paramMap);
        obj=JSONObject.parseObject(json);
        HttpUtils.getInstance().sendPost_JSON(url,obj);

    }
}
